<template>
  <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" append-to-body v-dialogDrag width="90%">
    <el-form :model="form" ref="form" :rules="rules" size="small" label-width="150px">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="工程名称" prop="gongchengName">
            <el-input v-model="form.gongchengName" placeholder="请输入工程名称"  clearable />
          </el-form-item>
        </el-col>


        <el-col :span="12">
          <el-form-item label="选择产品" prop="goodsId">
            <el-select v-model="form.goodsId" placeholder="请选择产品"
                       remote reserve-keyword :remote-method="searchGoods" @change="selectGoods" filterable clearable>
              <el-option v-for="item in goodsList"
                         :key="item.id" :label="item.label" :value="item.id"/>
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="存货编码" prop="goodsCode">
            <el-input v-model="form.goodsCode" placeholder="请输入存货编码" disabled clearable />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="产品名称" prop="goodsName">
            <el-input v-model="form.goodsName" placeholder="请输入产品名称" disabled clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="产品型号" prop="goodModel">
            <el-input v-model="form.goodModel" placeholder="请输入产品型号" disabled clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="合同物料名称" prop="contractGoodsName">
            <el-input v-model="form.contractGoodsName" placeholder="请输入合同物料名称"  clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="项目单位" prop="goodsName">
            <el-input v-model="form.projectDept" placeholder="请输入项目单位"  clearable />
          </el-form-item>
        </el-col>
        <!--<el-col :span="12">
          <el-form-item label="买方名称">
            <el-input v-model="form.order.customName" disabled placeholder="请输入买方名称" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="业务员" >
            <el-input v-model="form.order.salesmanName" disabled placeholder="请输入业务员" clearable />
          </el-form-item>
        </el-col>-->
        <el-col :span="12">
          <el-form-item label="产品单位" prop="unit">
            <el-input v-model="form.unit" disabled placeholder="请输入产品单位" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="销售数量" prop="orderCount">
            <el-input-number v-model="form.orderCount"  style="width: 200px;"  :precision="4" @change="handleChangeMoneyEdit(form.orderCount)" :min="0" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="销售单价（不含税）" prop="goodsUnitPrice">
            <el-input-number v-model="form.goodsUnitPrice" style="width: 200px;"  :precision="4" @change="handleChangeMoneyEdit(form.goodsUnitPrice)" :min="0" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="销售单价（含税）" prop="goodsTaxUnitPrice">
            <el-input-number v-model="form.goodsTaxUnitPrice" style="width: 200px;"  :precision="4"  @change="handleChangeMoneyEdit(form.goodsTaxUnitPrice)" :min="0" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="销售总价（不含税）" prop="goodsTotalPrice">
            <el-input-number v-model="form.goodsTotalPrice" style="width: 200px;"  :min="0" :precision="4"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="销售总价（含税）" prop="goodsTaxTotalPrice">
            <el-input-number v-model="form.goodsTaxTotalPrice" style="width: 200px;"  :min="0" :precision="4" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="需求日期" prop="deliveryTime">
            <el-date-picker v-model="form.deliveryTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择需求日期" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" clearable />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="dialog-footer" slot="footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {accAdd, addAction, getAction,deleteAction, updateAction, getById,accMul} from '@/api/manage'
export default {
  name: "AddGoodsAgainForm",
  dicts: ['goods_status', 'isdo', 'goods_order_in'],
  data() {
    return {
      // 弹出层标题
      title: "新增产品信息",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      goodsList:[],
      // 表单校验
      rules: {
        goodsId: [{ required: true, message: "产品编码不能为空", trigger: "blur" }],
        goodsCode: [{ required: true, message: "产品编码不能为空", trigger: "blur" }],
        orderCount: [{ required: true, message: "订单数量不能为空", trigger: "blur" }],
        goodsUnitPrice: [{ required: true, message: "单价不能为空", trigger: "blur" }],
        goodsTaxUnitPrice: [{ required: true, message: "含税单价不能为空", trigger: "blur" }],
        goodsTotalPrice: [{ required: true, message: "销售总价（不含税）不能为空", trigger: "blur" }],
        goodsTaxTotalPrice: [{ required: true, message: "销售总价（含税）不能为空", trigger: "blur" }],
      },
      url: {
          addAgain:'/goodsSale/goodsSale/addAgain',
          edit: '/goodsSale/goodsSale/edit',
          info: '/goodsSale/goodsSale/',
          goodsOrderInUrl: '/goodsOrderIn/goodsOrderIn/listOptions',
          goodsOrderInById: '/goodsOrderIn/goodsOrderIn/',
      },
      goodsOrderInOptions:[],
    };
  },
  created() {

  },
  methods: {

    selectGoodsOrderIn(value) {
      let that = this;
      let arr = that.$refs.mySelectGoodsOrderIn;
      for (let i = 0; i < arr.length; i++) {
        if (arr[i].currentValue == value) {
          that.form.goodsCode = arr[i].label;
          /*that.form.goodsName = arr[i].remark;
          that.form.goodModel = arr[i].$attrs.data.dictType;*/
          getById(this.url.goodsOrderInById, value).then(response => {
            //this.form = response.data;
            that.form.goodsName = response.data.name;
            that.form.goodModel = response.data.model;
            that.form.unit = response.data.unit;
            this.$forceUpdate();
          });
        }
      }
    },

    handleChangeMoneyEdit(row) {
      let that = this;
      let handleReckonMoneyEdit = (row) => {
        let row_money = 0;
        let row_tax_money = 0;
        row_money = accMul(Number(that.form.goodsUnitPrice), Number(that.form.orderCount))
        that.$set(that.form, 'goodsTotalPrice', row_money);
        row_money = accMul(Number(that.form.goodsUnitPrice), Number(1.13))
        that.$set(that.form, 'goodsTaxUnitPrice', row_money);
        row_tax_money = accMul(Number(that.form.goodsTaxUnitPrice), Number(that.form.orderCount))
        that.$set(that.form, 'goodsTaxTotalPrice', row_tax_money);
      };
      handleReckonMoneyEdit(row);
    },
    show(row) {
      this.form = {};
      this.form.projectId=row.id;
      this.form.projectNo=row.code;
      this.form.customerId=row.customId;
      this.form.customerName=row.customName;
      this.form.saleId=row.salesmanId;
      this.form.saleName=row.salesmanName;
      this.open = true;
    },

    searchGoods(query) {
      if (query !== '') {
        this.loading = true; // 开始加载数据
        let params = {"queryLabel": query, "pageNum": 1, "pageSize": 50,};
        getAction('/goodsOrderIn/goodsOrderIn/list', params).then(response => {
          this.goodsList = response.rows;
          for (let i = 0; i < this.goodsList.length; i++) {
            this.goodsList[i].label = this.goodsList[i].name + '-' + this.goodsList[i].model + '-' + this.goodsList[i].code;
          }
        })
      } else {
        this.goodsList = [];
      }
    },

    selectGoods(value) {
      for (let i = 0; i < this.goodsList.length; i++) {
        if (this.goodsList[i].id == value) {
          this.form.goodsId = this.goodsList[i].id;
          this.form.goodsCode = this.goodsList[i].code;
          this.form.goodsName = this.goodsList[i].name;
          this.form.goodModel = this.goodsList[i].model;
          this.form.unit = this.goodsList[i].unit;
          this.$forceUpdate();
        }
      }
    },

    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
            if(!this.form.id) {
              addAction(this.url.addAgain, this.form).then(response => {
                this.$emit('ok', this.form);
                this.open = false;
              });
            } else {
              updateAction(this.url.edit, this.form).then(response => {
              this.$emit('ok', this.form);
                this.open = false;
              });
            }
          // 这里可以添加提交逻辑
        }
      });
    },
    cancel() {
      this.open = false;
    },
  }
};
</script>
